home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / dir.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  133 lines

  1.  
  2. /*
  3.  *  OPENDIR.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <dos/dos.h>
  12. #include <clib/dos_protos.h>
  13. #include <sys/dir.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #ifndef UnixToAmigaPath
  19. #define UnixToAmigaPath(path)   path
  20. #endif
  21.  
  22. typedef struct FileInfoBlock FileInfoBlock;
  23.  
  24. typedef struct {
  25.     struct DHan     *dh_Next;
  26.     FileInfoBlock   dh_Fib;
  27.     BPTR        dh_Lock;
  28.     struct direct   dh_Direct;
  29. } DHan;
  30.  
  31. DHan *DHBase = NULL;
  32.  
  33. static __autoexit void
  34. dir_exit()
  35. {
  36.     while (DHBase)
  37.     closedir((DIR *)DHBase);
  38. }
  39.  
  40.  
  41. DIR *
  42. opendir(path)
  43. const char *path;
  44. {
  45.     DHan *dh = NULL;
  46.     long lock;
  47.  
  48.     if (lock = Lock(UnixToAmigaPath(path), SHARED_LOCK)) {
  49.     if (dh = malloc(sizeof(DHan))) {
  50.         dh->dh_Lock = lock;
  51.         if (rewinddir((DIR *)dh) < 0) {
  52.         closedir((DIR  *)dh);
  53.         dh = NULL;
  54.         }
  55.         dh->dh_Next = (struct DHan *)DHBase;
  56.         DHBase= dh;
  57.     } else {
  58.         UnLock(lock);
  59.     }
  60.     }
  61.     return(dh);
  62. }
  63.  
  64. struct direct *
  65. readdir(di)
  66. DIR *di;
  67. {
  68.     DHan *dh = (DHan *)di;
  69.  
  70.     if (ExNext(dh->dh_Lock, &dh->dh_Fib)) {
  71.     dh->dh_Direct.d_name = dh->dh_Fib.fib_FileName;
  72.     dh->dh_Direct.d_namlen = strlen(dh->dh_Fib.fib_FileName);
  73.     return(&dh->dh_Direct);
  74.     }
  75.     return(NULL);
  76. }
  77.  
  78. int
  79. rewinddir(di)
  80. DIR *di;
  81. {
  82.     DHan *dh = (DHan *)di;
  83.     int r = -1;
  84.  
  85.     if (Examine(dh->dh_Lock, &dh->dh_Fib)) {
  86.     if (dh->dh_Fib.fib_DirEntryType > 0)
  87.         r = 0;
  88.     }
  89.     return(r);
  90. }
  91.  
  92. int
  93. closedir(di)
  94. DIR *di;
  95. {
  96.     DHan *dh;
  97.     DHan **dhp;
  98.     int r = -1;
  99.  
  100.     for (dhp = &DHBase; dh = *dhp; dhp = (DHan**)&dh->dh_Next)
  101.      {
  102.     if (dh == (DHan *)di) {
  103.         *dhp = (DHan *)dh->dh_Next;
  104.         if (dh->dh_Lock)
  105.         UnLock(dh->dh_Lock);
  106.         free(dh);
  107.         r = 0;
  108.         break;
  109.     }
  110.     }
  111.     return(r);
  112. }
  113.  
  114. #ifdef TEST
  115.  
  116. int
  117. main(ac, av)
  118. char *av[];
  119. {
  120.     DIR *dh;
  121.  
  122.     if (dh = opendir(av[1])) {
  123.     struct direct *en;
  124.     while (en = readdir(dh)) {
  125.         printf("READ: %s\n", en->d_name);
  126.     }
  127.     closedir(dh);
  128.     }
  129.     return(0);
  130. }
  131.  
  132. #endif
  133.